Editor.DraggerCanvas = function () {
	var background;
	var iniX;
	var iniY;
	var isActive = false;
	var rect;
	var x;
	var y;
	var width;
	var height;
	return {
		init: function () {},
		mouseover: function () {},
		mouseout: function () {},
		remove: function () {},
		draw: function () {
			Editor.STATE.BACKGROUND0 = Editor.STATE.CANVAS.rect(0, 0, Editor.CONFIG.CANVAS_WIDTH, Editor.CONFIG.CANVAS_HEIGHT, 0).attr(Editor.CONFIG.BACKGROUND_ATTR);
			if (Editor.CONFIG.GRID_DRAW) {
				var posx = 0;
				while (posx <= Editor.CONFIG.CANVAS_WIDTH) {
					var path = ["M", posx, 0, "L", posx, Editor.CONFIG.CANVAS_HEIGHT].join(",");
					Editor.STATE.CANVAS.path(path).attr(Editor.CONFIG.GRID_ATTR);
					posx += Editor.CONFIG.GRID_SIZE;
				}
				var posy = 0;
				while (posy <= Editor.CONFIG.CANVAS_WIDTH) {
					var path = ["M", 0, posy, "L", Editor.CONFIG.CANVAS_WIDTH, posy].join(",");
					Editor.STATE.CANVAS.path(path).attr(Editor.CONFIG.GRID_ATTR);
					posy += Editor.CONFIG.GRID_SIZE;
				}
			}
			background = Editor.STATE.CANVAS.rect(0, 0, Editor.CONFIG.CANVAS_WIDTH, Editor.CONFIG.CANVAS_HEIGHT, 0).attr(Editor.CONFIG.BACKGROUND_ATTR).attr(Editor.CONFIG.UNVISIBLE_ATTR);
			background.mousedown(Editor.STATE.DRAGGERCANVAS.mousedown);
			Editor.STATE.BACKGROUND = background;
		},
		mousedown: function (e) {
			var offsetY = $(window).scrollTop();
			var offsetX = $(window).scrollLeft();
			var pos = $("#canvas").position();
			if (!Editor.Util.isDragging()) {
				Editor.Util.unselectAll();
				Editor.STATE.DRAGGERCANVAS.setInitPoint(e.clientX + offsetX - pos.left, e.clientY + offsetY - pos.top);
				Editor.STATE.DRAGGER = Editor.STATE.DRAGGERCANVAS;
			}
		},
		onmousemove: function (e) {
			var offsetY = $(window).scrollTop();
			var offsetX = $(window).scrollLeft();
			var pos = $("#canvas").position();
			var nx = e.clientX + offsetX - pos.left;
			var ny = e.clientY + offsetY - pos.top;
			var dx = nx - x;
			var dy = ny - y;
			width = width + dx;
			height = height + dy;
			x = nx;
			y = ny;
			var sw = width;
			var sh = height;
			var tx = iniX - rect.getBBox().x;
			var ty = iniY - rect.getBBox().y;
			if (sw < 0) {
				sw = -1 * width;
				tx = (iniX - sw) - rect.getBBox().x;
			}
			if (sh < 0) {
				sh = -1 * height;
				ty = (iniY - sh) - rect.getBBox().y;
			}
			rect.translate(tx, ty);
			rect.attr({
				width: sw,
				height: sh
			})
		},
		onmouseup: function () {
			var bbox = rect.getBBox();
            Editor.STATE.SHAPES.forEach(function(pair) {
            if (!pair.value.isDeleted()) {
					var shape = pair.value;
					if (Editor.Geometry.intersectShapes(bbox.x,bbox.y,bbox.width,bbox.height,shape.getX(),shape.getY(),shape.getWidth(),shape.getHeight())) {
						Editor.Util.select(shape);
					}
        }
      });
			
			rect.remove();
			delete rect;
			isActive = false;
			Editor.STATE.DRAGGER = undefined;
		},
		setInitPoint: function (nx, ny) {
			iniX = nx;
			iniY = ny;
			x = nx;
			y = ny;
			width = 0;
			height = 0;
			rect = Editor.STATE.CANVAS.rect(iniX, iniY, width, height, 0).attr(Editor.CONFIG.DRAGGERCANVAS_RECT_ATTR);
			isActive = true;
		},
		isActive: function () {
			return isActive;
		}
	}
}
